package com.dlyk.web;

import com.dlyk.model.TClue;
import com.dlyk.model.TProduct;
import com.dlyk.query.ProductQuery;
import com.dlyk.query.UserQuery;
import com.dlyk.result.R;
import com.dlyk.service.ProductService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
public class ProductController {

    @Resource
    private ProductService productService;

    /**
     * 查询在售的产品
     *
     * @return
     */
    @GetMapping(value = "/api/product/sale")
    public R saleProduct() {
        List<TProduct> tProductList = productService.getAllOnSaleProduct();
        return R.OK(tProductList);
    }

    /**
     * @description: 产品列表分页查询
     * @params: [current]
     * @return:
     * @author: ajie
     * @date: 2024/7/24 20:47
     */
    @GetMapping("api/products")
    public R productPage(@RequestParam(value = "current",required = false) Integer current,ProductQuery productQuery){
        if (current == null){
            current = 1;
        }
        PageInfo<TProduct> pageInfo = productService.getProductByPage(current,productQuery);
        return R.OK(pageInfo);
    }

    /**
     * @description: 根据id查询产品详情
     * @params: [id]
     * @return:
     * @author: ajie
     * @date: 2024/7/27 23:54
     */
    @GetMapping("api/product/detail/{id}")
    public R loadProduct(@PathVariable(value = "id") Integer id){
        TProduct tProduct = productService.getProductById(id);
        return R.OK(tProduct);
    }

    /**
     * @description: 新增产品
     * @params: [userQuery]
     * @return:
     * @author: ajie
     * @date: 2024/7/15 21:25
     */
    @PostMapping("api/product")
    public R addProduct(ProductQuery productQuery, @RequestHeader(value = "Authorization") String token){
        productQuery.setToken(token);
        int save = productService.saveProduct(productQuery);
        return save >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * @description: 编辑产品
     * @params: [userQuery]
     * @return:
     * @author: ajie
     * @date: 2024/7/15 21:25
     */
    @PutMapping("api/product")
    public R editProduct(ProductQuery productQuery,@RequestHeader(value = "Authorization") String token){
        productQuery.setToken(token);
        int update = productService.updateProduct(productQuery);
        return update >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * @description: 删除用户
     * @params: [id]
     * @return:
     * @author: ajie
     * @date: 2024/7/17 18:14
     */
    @DeleteMapping("api/product/{id}")
    public R delProduct(@PathVariable(value = "id") Integer id){
        int del = productService.delProductById(id);
        return del >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * @description: 批量删除用户
     * @params: [ids]
     * @return:
     * @author: ajie
     * @date: 2024/7/18 18:39
     */
    @DeleteMapping("api/product")
    public R batchDelProduct(@RequestParam(value = "ids") String ids){
        //ids = "1,3,5,7";
        List<String> idList = Arrays.asList(ids.split(","));
        int batchDel = productService.batchDelProductByIds(idList);
        return batchDel >= idList.size() ? R.OK() : R.FAIL();
    }
}